/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.hostess.persistence;

import br.hostess.model.Evento;
import br.hostess.model.Participante;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Marcelo Daibert
 */
public class ParticipanteDAO {

    public boolean salvar(Participante pParticipante) {
        System.out.println("Persistindo Objeto: " + pParticipante.toString());
        Connection objConexao = FabricaConexao.getConexao();
        try {
            Statement objSTM = objConexao.createStatement();
            //System.out.println("ID: "+pParticipante.getEvento().getOid());
            objSTM.execute("INSERT INTO tbl_participante(oid, nome, barras, fk_evento, colaborador) VALUES('" + pParticipante.getOid() + "','" + pParticipante.getNome() + "'," + pParticipante.getCodigobarras() + ",'" + pParticipante.getEvento().getOid() + "', 0)");
            objSTM.close();
            return true;
        } catch (Exception erro) {
            String errorMsg = "Erro ao Persistir: " + erro.getMessage();
            JOptionPane.showMessageDialog(null, errorMsg, "Mensagem", JOptionPane.ERROR_MESSAGE);
            return false;
        }

    }
    
    public static ArrayList<Participante> recuperaObjetosPorEvento(String pID) {
        Connection objConexao = FabricaConexao.getConexao();
        ArrayList<Participante> vetEventos = new ArrayList<Participante>();
        try {
            Statement objSTM = objConexao.createStatement();
            objSTM.executeQuery("SELECT oid, nome, barras FROM tbl_participante where fk_evento = '"+pID+"' order by nome ASC");
            ResultSet objResultSet = objSTM.getResultSet();
            Participante objBuffer;
            while (objResultSet.next()) {
                String vID = objResultSet.getString("oid");
                String vNome = objResultSet.getString("nome");
                int vBarras = objResultSet.getInt("barras");
                objBuffer = new Participante();
                objBuffer.setOid(vID);
                objBuffer.setNome(vNome);
                objBuffer.setCodigobarras(vBarras);
                vetEventos.add(objBuffer);
            }
            objResultSet.close();
            objSTM.close();
        } catch (Exception erro) {
            String errorMsg = "Erro ao Recuperar: " + erro.getMessage();
            //JOptionPane.showMessageDialog(null, errorMsg, "Mensagem", JOptionPane.ERROR_MESSAGE);
        }
        return vetEventos;
    }
    public static boolean excluir(String pOID) {
        System.out.println("Excluindo Objeto: " + pOID);
        Connection objConexao = FabricaConexao.getConexao();
        try {
            Statement objSTM = objConexao.createStatement();
            objSTM.execute("DELETE FROM tbl_participante WHERE oid = '"+pOID+"'");
            objSTM.close();
            return true;
        } catch (Exception erro) {
            String errorMsg = "Erro ao Deletar: " + erro.getMessage();
            //JOptionPane.showMessageDialog(null, errorMsg, "Mensagem", JOptionPane.ERROR_MESSAGE);
            return false;
        }  
    }
    public static String recuperaObjetoNome(String pID) {
        Connection objConexao = FabricaConexao.getConexao();
        Evento objBuffer = null;
        String vNome = null;
        try {
            Statement objSTM = objConexao.createStatement();
            objSTM.executeQuery("SELECT nome FROM tbl_participante WHERE oid = '"+pID+"' or barras = '"+pID+"'");
            ResultSet objResultSet = objSTM.getResultSet();
            while (objResultSet.next()) {
                vNome = objResultSet.getString("nome");
            }
            objResultSet.close();
            objSTM.close();
        } catch (Exception erro) {
            String errorMsg = "Erro ao Recuperar: " + erro.getMessage();
            JOptionPane.showMessageDialog(null, errorMsg, "Mensagem", JOptionPane.ERROR_MESSAGE);
        }
        return vNome;
    }  
    public static Participante recuperaObjeto(String pID) {
        Connection objConexao = FabricaConexao.getConexao();
        Participante objBuffer = null;
        try {
            Statement objSTM = objConexao.createStatement();
            objSTM.executeQuery("SELECT oid, nome, barras FROM tbl_participante WHERE oid = '"+pID+"' or barras = '"+pID+"'");
            ResultSet objResultSet = objSTM.getResultSet();
            while (objResultSet.next()) {
                String vID = objResultSet.getString("oid");
                String vNome = objResultSet.getString("nome");
                int vBarras = objResultSet.getInt("barras");
                objBuffer = new Participante();
                objBuffer.setOid(vID);
                objBuffer.setNome(vNome);
                objBuffer.setCodigobarras(vBarras);
            }
            objResultSet.close();
            objSTM.close();
        } catch (Exception erro) {
            String errorMsg = "Erro ao Recuperar: " + erro.getMessage();
            JOptionPane.showMessageDialog(null, errorMsg, "Mensagem", JOptionPane.ERROR_MESSAGE);
        }
        return objBuffer;
    }    
    

}
